package com.xlabs.client;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.xlabs.client.DO.PMF;
import com.xlabs.client.DO.QuestionDO;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Video data object.
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Video {
    /**
     * Primary key.
     */
    /**
     * Primary key.
     */
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private com.google.appengine.api.datastore.Key key;

    /**
     * Youtube video Id.
     */
    @Persistent
    private String videoId;

    /**
     * Comments on this video.
     */
    @Persistent
    private List<Key> comments;

    /**
     * Initializes an instance of Video
     * @param videoId youtube video id.
     */
    public Video(String videoId) {
        this.videoId = videoId;
    }

    /**
     * Gets the key.
     *
     * @return key.
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     * @param value the value for key.
     */
    public void setKey(Key value)
    {
        this.key = value;
    }

    /**
     * Gets videoId.
     *
     * @return videoId
     */
    public String getVideoId() {
        return videoId;
    }

    /**
     * Sets videoId.
     *
     * @param videoId videoId.
     */
    public void setVideoId(String videoId) {
        this.videoId = videoId;
    }

    /**
     * Gets comments.
     * @return comments.
     */
    public List<Key> getQuestions() {
        return comments;
    }

    /**
     * Sets comments.
     * @param comments video comments.
     */
    public void setQuestions(List<Key> comments) {
        this.comments = comments;
    }

    /**
     * Adds a comment to the video.
     * @param questionKey comment key.
     */
    public void addQuestion(Key questionKey)
    {
        if (this.comments == null)
        {
            this.comments = new ArrayList<Key>();
        }

        this.comments.add(questionKey);
    }

    /**
     * Creates video object if it doesn't exist and returns the video data object.
     *
     * @param videoId id of the video to be added.
     * @return video added or retrieved with the given videoId.
     */
    static Video AddVideo(String videoId) {

        PersistenceManager pm = PMF.get().getPersistenceManager();

        try {
            String videoExists = "select from " + Video.class.getName() + " where videoId == '" + videoId + "'";

            List<Video> videoResults = (List<Video>) pm.newQuery(videoExists).execute();

            if (videoResults.size() == 0) {
                Video newVideo = new Video(videoId);
                pm.makePersistent(newVideo);
                return newVideo;
            }

            return videoResults.get(0);

        } finally {
            pm.close();
        }
    }

    /**
     * Adds a comment to the video.
     *
     * @param questionText        comment.
     * @param time           time to be added.
     * @param currentVideoId video id.
     */
    public static void addQuestionToVideo(String questionText, int time, String currentVideoId) {

        PersistenceManager pm = PMF.get().getPersistenceManager();

        try {

            Video currentVideo = AddVideo(currentVideoId);

            QuestionDO newQuestionDO = new QuestionDO(questionText, time, time, new Date());

            newQuestionDO.setVideo(currentVideo.getKey());
            
            currentVideo.addQuestion(newQuestionDO.getKey());

            UserService userService = UserServiceFactory.getUserService();
            newQuestionDO.setCreatedBy(userService.getCurrentUser());

            pm.makePersistent(newQuestionDO);

        }
        finally {

            pm.close();
        }
    }
}
